#ifndef MWIGET_H
#define MWIGET_H

#include <QWidget>
#include <QVector>
#include <QPoint>
#include <QPaintEvent>
#include <QKeyEvent>
#include <atomic>

class Line
{
public:
    Line()
        :A(0.),B(0.),C(0.)
    {}
    Line(double a,double b,double c)
        :A(a),B(b),C(c)
    {}
    Line(double a,double b,QPointF point)
        : A(a), B(b)
    {
        C = -(A*point.x() + B*point.y());
    }

    Line(QPointF a,QPointF b)
    {
        A = a.y() - b.y();
        B = b.x() - a.x();
        C = a.x()*b.y() - b.x()*a.y();
    }

    QPointF cross(const Line& other)
    {
        double D = A*other.getB() - other.getA()*B;
        if(std::abs(D)<1e-4) return QPointF(-M_PI,-M_PI); //!< parallel
        else{
            double D_inv = 1/D;
            double x = (B*other.getC() - other.getB()*C) * D_inv;
            double y = (C*other.getA() - other.getC()*A) * D_inv;
            return QPointF(x,y);
        }
    }

    double getA() const
    {
        return A;
    }
    double getB() const
    {
        return B;
    }
    double getC() const
    {
        return C;
    }

protected:
    double A,B,C;
};

class MWiget : public QWidget
{
    Q_OBJECT
public:
    explicit MWiget(QWidget *parent = nullptr);
    int init();
    void keyPressEvent(QKeyEvent *event);

    int solve();
signals:

public slots:

protected:
    void paintEvent(QPaintEvent *);

    const double R;
    const double centerX;
    const double centerY;
    const int    dataNum;

    QVector<QPointF> QPointData;
    QPointF point01,point02,centerResult;
    double R_Result;
    QPointF point0,point1,point2;
    Line first,second;
    std::atomic_bool isUpdated;
};

#endif // MWIGET_H
